package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemCompanyNameTextBinding;
import com.example.yichuangyun.model.business.CompanyModel;
import com.example.yichuangyun.mvvm.ui.activity.CompanyDetailsActivity;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;

import java.util.ArrayList;
import java.util.List;

public class CompanyNameAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<CompanyModel> mData;

    public CompanyNameAdapter(Context context, List<CompanyModel> mData) {
        this.context = context;
        this.mData = mData;
    }

    public void onRefresh(List<CompanyModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        ItemCompanyNameTextBinding binding = ItemCompanyNameTextBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CompanyModel model = mData.get(position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.binding.tvCompanyName.setText(CommonUtil.checkText(model.getCompName()));

        myViewHolder.binding.tvCompanyName.setOnClickListener(view -> {
            if (CommonUtil.isCanClick()) {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.creditNoKey, model.getCreditNo());
                JumpIntent.jump((Activity) context, CompanyDetailsActivity.class, bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemCompanyNameTextBinding binding;

        public MyViewHolder(ItemCompanyNameTextBinding itemCompanyNameTextBinding) {
            super(itemCompanyNameTextBinding.getRoot());
            binding = itemCompanyNameTextBinding;
        }
    }
}
